package com.wx.jpa.service2.impl;

import com.wx.jpa.entity2.UserInfo2;
import com.wx.jpa.respository2.UserRepository2;
import com.wx.jpa.service2.UserService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl2
 * @Author wx
 * @Description 用户业务实现层2
 * @Date 2018-12-26-22:47
 */
@Service
public class UserServiceImpl2 implements UserService2{
    @Autowired
    private UserRepository2 userRepository2;

    /**
     * @methodName: getUserList
     * @author: wx
     * @description: 获取所有用户列表
     * @param
     * @date: 2018/12/26
     * @return: java.util.List<com.wx.jpa.entity.UserInfo>
     */
    @Override
    public List<UserInfo2> getUserList() {
        return  userRepository2.findAll();
    }

    /**
     * @methodName: getUserByName
     * @author: wx
     * @description: 通过姓名获取用户信息
     * @param name 用户姓名
     * @date: 2018/12/26
     * @return: com.wx.jpa.entity.UserInfo
     */
    @Override
    public UserInfo2 getUserByName(String name) {
        return userRepository2.findByName(name);
    }

    /**
     * @methodName: addUserInfo
     * @author: wx
     * @description:  新增用户信息
     * @param userInfo 用户信息
     * @date: 2018/12/26
     * @return: com.wx.jpa.entity.UserInfo
     */
    @Override
    public UserInfo2 addUserInfo(UserInfo2 userInfo) {
        return userRepository2.save(userInfo);
    }

    /**
     * @methodName: updateUserInfoById
     * @author: wx
     * @description: 更新用户信息
     * @param userInfo 用户信息
     * @date: 2018/12/26
     * @return: com.wx.jpa.entity.UserInfo
     */
    @Override
    public UserInfo2 updateUserInfoById(UserInfo2 userInfo) {
        return userRepository2.save(userInfo);
    }

    /**
     * @methodName: deleteUserInfoById
     * @author: wx
     * @description: 根据id删除用户
     * @param id
     * @date: 2018/12/26
     * @return: void
     */
    @Override
    public void deleteUserInfoById(Long id) {
        userRepository2.deleteById(id);
    }

    /**
     * @methodName: getCurrentUserList
     * @author: wx
     * @description: 获取最新的用户
     * @param
     * @date: 2018/12/26
     * @return: java.util.List<com.wx.jpa.entity.UserInfo>
     */
    @Override
    public List<UserInfo2> getCurrentUserList() {
        Sort sort=new Sort(Sort.Direction.DESC,"createTime");
        return userRepository2.findAll(sort);
    }

    /**
     * @methodName: getPageUserList
     * @author: wx
     * @description: 获取分页的用户
     * @param
     * @date: 2018/12/26
     * @return: org.springframework.data.domain.Page<com.wx.jpa.entity.UserInfo>
     */
    @Override
    public Page<UserInfo2> getPageUserList() {
        Sort sort=new Sort(Sort.Direction.DESC,"createTime");
        Pageable pageable=new PageRequest(0,5,sort);
        return userRepository2.findAll(pageable);
    }
}
